package q503;

/**
 * 503 下一个更大的元素
 */
public class Solution {

    public int[] nextGreaterElements(int[] nums) {

        int[] res = new int[nums.length];

        for (int i = 0; i < nums.length; i++) {
            int target = nums[i];

            int j = i + 1;

            boolean flag = false;
            while (true) {
                if (j == nums.length) {
                    j = 0;
                }
                if (j == i) {
                    break;
                }
                if (nums[j] > target) {
                    res[i] = nums[j];
                    flag = true;
                    break;
                }
                j += 1;
            }

            if (!flag) {
                res[i] = -1;
            }
        }

        return res;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{5,4,3,2,1};
        int[] ints = solution.nextGreaterElements(nums);
        System.out.println(ints);
    }
}
